package com.dingding.service.usermangerservice.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.databean.request.ReqGetUserBalanceList;
import com.dingding.bean.databean.request.ReqGetUserDepositList;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResGetUserBalanceList;
import com.dingding.bean.databean.response.ResGetUserDepositList;
import com.dingding.bean.entity.Depositdetail;
import com.dingding.bean.entity.Useraccountdetail;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.BaseService;
import com.dingding.service.usermangerservice.IUserAccountDetailService;

/**
 * 
 * @ClassName: IUserAccountDetailService
 * @Description:用户账户明细service
 * @author cheshaowei
 * @date 2015-10-21 上午10:36:39
 */

public class UserAccountDetailServiceImpl extends BaseService implements
		IUserAccountDetailService {

	// 日志
	private Logger log = Logger.getLogger(UserAccountDetailServiceImpl.class);

	/**
	 * @Title: getUserBalanceList
	 * @Description: 获取用户账户明细
	 * @param userID
	 *            用户id
	 * @param orderType
	 *            订单类型 1：已完成 2：已取消
	 * @param pageSize
	 *            每页个数
	 * @param pageIndex
	 *            当前页
	 * @author zhaochenguang
	 * @return List<ResgetUserBalanceList> 响应信息封装
	 * @throws
	 * @Override
	 */
	@Transactional(readOnly = true)
	public void getUserBalanceList(ReqGetUserBalanceList reqGetUserBalanceList,
			DataResponse dataResponse) {

		// 验证获取用户账户明细时客户端传入参数是否为空
		boolean flag = this.validateGetUserBalanceList(reqGetUserBalanceList);

		// 声明响应bean数组
		List<ResGetUserBalanceList> resList = new ArrayList<ResGetUserBalanceList>();
		
		if (!flag) {
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			// 设置响应信息body
			dataResponse.setResponseBody(resList);
			return;
		}
		
		//创建用户存储用户账户明细对象
		List<Useraccountdetail> userAccountDetailList = null;

		// 设置查询条件：按照用户账户id进行查找
		Criterion criterion = Restrictions.eq("userId",
				reqGetUserBalanceList.getUserId());

		try {

			// 按照用户id,交易类型，每页个数与当前页进行查询用户账户明细
			userAccountDetailList = super.userAccountDetailDao
					.queryCriteriaListByPage(criterion, "tradeTime",
							reqGetUserBalanceList.getPageIndex(),
							reqGetUserBalanceList.getPageSize(), false);
			
		} catch (Exception e) {

			log.error(
					"获取用户账户明细出现异常！UserAccountDetailServiceImpl 方法:getUserBalanceList()",
					e);
			setStatus(dataResponse, SystemCode.ERROR);
			// 设置响应信息body
			dataResponse.setResponseBody(resList);
			return;
		}

		// 判断有没有记录
		if (userAccountDetailList == null || userAccountDetailList.size() <= 0) {
			setStatus(dataResponse, SystemCode.SUCCESS);
			// 设置响应信息body
			dataResponse.setResponseBody(resList);
			return;
		}

		
		// 转换用户账户明细bean为响应bean
		super.userAccountDetailDao.setResGetUserBalanceList(
				userAccountDetailList, resList);
		// 设置响应信息body
		dataResponse.setResponseBody(resList);
		// 设置响应：响应码，响应信息
		setStatus(dataResponse, SystemCode.SUCCESS);

	}

	/**
	 * @Title: getUserDepositList
	 * @Description: 获取用户提现明细
	 * @param reqGetUserDepositList
	 * @param dataResponse
	 * @author lijinchao
	 * @return void
	 * @throws
	 */
	@Transactional(readOnly = true)
	public void getUserDepositList(ReqGetUserDepositList reqGetUserDepositList,
			DataResponse dataResponse) {

		// 检验请求参数
		boolean flag = this.validateGetUserDepositList(reqGetUserDepositList);
		// 请求参数为空
		if (!flag) {
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		//创建用于存储体现明细的对象
		List<Depositdetail> depositdetails = null;

		//设置查询条件：将用户Id作为查询条件进行查询
		Criterion criterion = Restrictions.eq("userId",
				reqGetUserDepositList.getUserId());

		try {
			depositdetails = super.depositDetailDao.queryCriteriaListByPage(
					criterion, "depositTime",
					reqGetUserDepositList.getPageIndex(),
					reqGetUserDepositList.getPageSize(), false);
		} catch (Exception e) {

			log.error("获取用户提现明细出现异常！UserAccountDetailServiceImpl  方法:getUserDepositList()", e);

			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}
		
		// 声明响应bean数组
		List<ResGetUserDepositList> resList = new ArrayList<ResGetUserDepositList>();
		
		// 转换用户账户明细bean为响应bean
		super.userAccountDetailDao.setResGetUserDepositList(depositdetails,
				resList);
		// 设置响应信息body
		dataResponse.setResponseBody(resList);
		// 设置响应：响应码，响应信息
		setStatus(dataResponse, SystemCode.SUCCESS);

	}

	/**
	 * @Title: validateGetUserBalanceList
	 * @Description: 验证获取用户账户明细时传入的参数是否为空
	 * @param reqGetUserBalanceList
	 *            客户端传入参数
	 * @return true 参数正确 false 参数有误
	 * @throws
	 */
	public boolean validateGetUserBalanceList(
			ReqGetUserBalanceList reqGetUserBalanceList) {

		if (reqGetUserBalanceList == null // 获取用户明细时传入参数是否为空
				|| ToolUtil.isBlank(reqGetUserBalanceList.getUserId()) // 用户Id是否为空
				|| reqGetUserBalanceList.getPageIndex() == null // 当前页是否为空
				|| reqGetUserBalanceList.getPageSize() == null) { // 每页显示记录数是否为空

			return false;
		}

		return true;
	}

	/**
	 * @Title: validateGetUserDepositList
	 * @Description: 验证获取用户提现明细时传入的参数是否为空
	 * @param reqGetUserDepositList
	 * @author lijinchao
	 * @return true 参数正确  false 参数有误
	 * @throws
	 */
	private boolean validateGetUserDepositList(
			ReqGetUserDepositList reqGetUserDepositList) {

		if (reqGetUserDepositList == null
				|| ToolUtil.isBlank(reqGetUserDepositList.getUserId()) // 用户id
				|| reqGetUserDepositList.getPageIndex() == null // 当前页数
				|| reqGetUserDepositList.getPageSize() == null) { // 每页个数

			return false;
		}

		return true;
	}

	@Override
	public void setStatus(DataResponse dataResponse, int status) {

		switch (status) {
		// 成功
		case SystemCode.SUCCESS:
			dataResponse.setResponseCode(SystemCode.SUCCESS);
			dataResponse.setResponseMessage(SystemCode.SUCCESS_MESSAGE);
			break;
		// 错误
		case SystemCode.ERROR:
			dataResponse.setResponseCode(SystemCode.ERROR);
			dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
			break;
		// 客户端传递的参数为空
		case SystemCode.DATA_NULL_ERROR:
			dataResponse.setResponseCode(SystemCode.DATA_NULL_ERROR);
			dataResponse.setResponseMessage(SystemCode.DATA_NULL_MESSAGE);
			break;
		// 客户端传递的参数为空
		case SystemCode.NOT_DEPOSIT_ERROR:
			dataResponse.setResponseCode(SystemCode.NOT_DEPOSIT_ERROR);
			dataResponse.setResponseMessage(SystemCode.NOT_DEPOSIT_ERROR_MESSAGE);
			break;
			
		// 默认
		default:
			dataResponse.setResponseCode(SystemCode.ERROR);
			dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
			break;
		}

	}

}
